package com.lv.service.room.company

import android.content.Context
import android.os.Parcelable
import androidx.annotation.Keep
import androidx.annotation.Nullable
import androidx.lifecycle.LiveData
import androidx.room.*
import com.lv.service.bean.*
import com.lv.service.net.CategoryPeriodsBody
import com.lv.service.room.helper.*
import kotlinx.parcelize.Parcelize

/**
 * 作者： ygx
 * 创建日期：2022/1/8 2:42 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * App的公共业务基础数据类
 */

//3、database
@Database(entities = [CompanyDraftCommonItemBean::class], version = 1, exportSchema = false)
abstract class CompanyDraftCommonDatabase : RoomDatabase() {

    abstract val companyDao: CompanyDraftCommonDao

    companion object {

        private const val USER_DB_NAME = "syy_o_company_draft_common_db"

        @Volatile
        private var instance: CompanyDraftCommonDatabase? = null

        @Synchronized
        fun getInstance(context: Context): CompanyDraftCommonDatabase {
            return instance ?: Room.databaseBuilder(
                context,
                CompanyDraftCommonDatabase::class.java,
                USER_DB_NAME
            ).build().also { instance = it }
        }
    }
}


//1、entity的定义
@TypeConverters(
    InspectionTvivConverter::class,
    InspectionStringConverter::class,
    InspectionCaegoryPeriodsConverter::class
)
@Parcelize
@Entity(tableName = "company_draft_common_db")
@Keep
data class CompanyDraftCommonItemBean(
    @PrimaryKey(autoGenerate = false)
//    var _id: Int = 0,//主键
    var id: String,//主键
    @Nullable
    var logoMedia: List<String>?,
    @Nullable
    var name: String?,
    @Nullable
    var identification: String?,
    @Nullable
    var phone: String?,//登录号码
    @Nullable
    var bnames: String?,
    @Nullable
    var province: String?,
    @Nullable
    var city: String?,
    @Nullable
    var area: String?,
    @Nullable
    var prefecture: String?,
    @Nullable
    var address: String?,
    @Nullable
    var category: List<CategoryPeriodsBody>?,
    @Nullable
    var factoryMedia: List<TVIVItemBean>?,
    @Nullable
    var userId: String?,
    @ColumnInfo(name = "operation_order")
    var order: Long
) : Parcelable

////1、entity的定义
//
//@Entity(tableName = "tb_cniao_user")
//data class CniaoUserInfo(
//    @PrimaryKey
//    val id: Int,//主键
//    val course_permission: Boolean,
//    val token: String?,
//    @Embedded
//    val user: User?
//) {
//    @Keep
//    data class User(
//        @ColumnInfo(name = "cniao_user_id")
//        val id: Int,//用户id
//        val logo_url: String?,//用户头像
//        val reg_time: String?,//用户注册时间
//        val username: String?//用户名
//    )
//}

//2、dao层的定义
@Dao
interface CompanyDraftCommonDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertFindCompany(companyItemBean: CompanyDraftCommonItemBean)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertFindSuspendCompany(companyItemBean: CompanyDraftCommonItemBean)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertFindSuspendCompany(companyItems: List<CompanyDraftCommonItemBean>)

    @Update(onConflict = OnConflictStrategy.REPLACE)
    fun updateFindCompany(companyItemBean: CompanyDraftCommonItemBean)

    @Delete
    fun deleteFindCompany(companyItemBean: CompanyDraftCommonItemBean)

    @Query("delete from company_draft_common_db")
    fun deleteAllCompany()

    @Query("delete from company_draft_common_db")
    suspend fun deleteSuspendAllCompany()

    // = , in , like
    @Query("select * from company_draft_common_db where id=:id")
    fun queryLiveFindCompany(id: Int = 0): LiveData<CompanyDraftCommonItemBean>

    @Query("select * from company_draft_common_db")
    fun queryLiveFindAllCompany(): LiveData<List<CompanyDraftCommonItemBean>>

    @Query("select * from company_draft_common_db where userId =:id")
    fun getLiveFindUserCompany(id: String): LiveData<List<CompanyDraftCommonItemBean>>

    @Query("select * from company_draft_common_db where userId =:id order by operation_order desc")
    fun getLiveFindUserSortCompany(id: String): LiveData<List<CompanyDraftCommonItemBean>>
//    @Query("select * from category_db where _id=:id")
//    fun queryFindCategory(id: Int = 0): CategoryBean?

//    @Query("select * from category_db")
//    fun queryFindAllCategory(): List<CategoryBean>?

    @Query("select * from company_draft_common_db where id =:id")
    suspend fun getFindCompany(id: Int = 0): CompanyDraftCommonItemBean?

    @Query("select * from company_draft_common_db where userId =:id")
    suspend fun getFindUserCompany(id: String): List<CompanyDraftCommonItemBean>?

    @Query("select * from company_draft_common_db")
    suspend fun getAllCompany(): List<CompanyDraftCommonItemBean>?
}



